<?php

namespace app\helpers;

class Wx
{//确认单制作
    const APP_ID = 'wx73c3026421c148bd';
    const SECRET = 'bb2247af7ee6e3d131babcff4dcd193b';
    const TOKEN = 'TourPig';

    public static function ticket()
    {
        $redis = Redis::getRedis();
        $ticket_key = 'wx_jsapi_ticket';
        if (false === $redis->get($ticket_key)) {
            $access_token = self::access_token();
            $ticket_res = json_decode(file_get_contents("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$access_token&type=jsapi"), true);
            $redis->set($ticket_key, $ticket_res['ticket'], ['NX', 'EX' => $ticket_res['expires_in']]);
        }
        return $redis->get($ticket_key);
    }

    public static function access_token()
    {
        $redis = Redis::getRedis();
        $token_key = 'wx_access_token';
        $app_id = self::APP_ID;
        $secret = self::SECRET;
        if (false === $redis->get($token_key)) {
            $access_token_res = json_decode(file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$app_id}&secret={$secret}"), true);
            $redis->set($token_key, $access_token_res['access_token'], ['NX', 'EX' => $access_token_res['expires_in']]);
        }
        return $redis->get($token_key);
    }
}